package cn.codingjc.peekaboo.application.core.sql.process;

import cn.codingjc.peekaboo.application.core.pipeline.BusinessProcess;
import cn.codingjc.peekaboo.application.core.pipeline.ProcessContext;
import cn.codingjc.peekaboo.application.core.sql.param.SqlSchemaParam;
import cn.codingjc.peekaboo.application.util.MessageUtils;
import cn.codingjc.peekaboo.domain.domain.sql.dto.TableSchemaDTO;
import cn.codingjc.peekaboo.domain.domain.sys.vo.ResultVO;
import cn.hutool.core.collection.CollUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 参数校验
 * @date: 2023/5/4
 * @author: Codingjc
 * @className: ValidSqlSchema
 **/
public class ValidSqlSchemaProcess implements BusinessProcess {
    @Override
    public void process(ProcessContext processContext) {
        SqlSchemaParam sqlSchemaParam = (SqlSchemaParam) processContext.getProcessParam();
        TableSchemaDTO tableSchemaDTO = sqlSchemaParam.getTableSchemaDTO();
        if (tableSchemaDTO == null) {
            processContext.setNeedBreak(true);
            processContext.setResult(ResultVO.fail(MessageUtils.getMessage("param.is.null")));
        }
        if (StringUtils.isEmpty(tableSchemaDTO.getTableName())) {
            processContext.setNeedBreak(true);
            processContext.setResult(ResultVO.fail(MessageUtils.getMessage("table.name.is.null")));
        }

//        if (tableSchemaDTO.getMockNum() > 100 || tableSchemaDTO.getMockNum() <= 0 ) {
//            processContext.setNeedBreak(true);
//            processContext.setResult(ResultVO.fail(MessageUtils.getMessage("mock.num.invalid")));
//        }

        if(CollUtil.isEmpty(tableSchemaDTO.getFieldList())) {
            processContext.setNeedBreak(true);
            processContext.setResult(ResultVO.fail(MessageUtils.getMessage("field.is.null")));
        }

        List<TableSchemaDTO.Field> fieldList = tableSchemaDTO.getFieldList();
        for (TableSchemaDTO.Field field : fieldList) {
            validField(field, processContext);
        }

    }

    private void validField(TableSchemaDTO.Field field, ProcessContext processContext) {
        String fieldName = field.getFieldName();
        String fieldType = field.getFieldType();
        if (StringUtils.isBlank(fieldName)) {
            processContext.setNeedBreak(true);
            processContext.setResult(ResultVO.fail(MessageUtils.getMessage("field.name.is.null")));
        }
        if (StringUtils.isBlank(fieldType)) {
            processContext.setNeedBreak(true);
            processContext.setResult(ResultVO.fail(MessageUtils.getMessage("field.type.is.null")));
        }
    }
}
